package com.sping.gobang.common;

import com.sping.gobang.exception.UserException;
import com.sping.gobang.pojo.response.Result;
import io.jsonwebtoken.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.resource.NoResourceFoundException;

import java.sql.SQLIntegrityConstraintViolationException;

@Slf4j
@ControllerAdvice
@ResponseBody  //相应是json格式
//自定义异常
public class ExceptionAdvice {
    @ExceptionHandler
    public Result exceptionHandler(Exception e){
        log.error("发生异常,e:",e);
        return Result.fail(e.getMessage());
    }
    @ExceptionHandler
    public Result exceptionHandler(HttpMediaTypeNotAcceptableException e){
        log.error("Jackson 无法序列化");
        return Result.fail(e.getMessage());
    }
    @ExceptionHandler
    public Result exceptionHandler(UserException e){
        log.error(e.getErrMsg());
//        log.error(e.getMessage());

        return Result.fail(e.getErrMsg());
    }
    @ExceptionHandler
    public Result exceptionHandler(HttpMessageNotReadableException e){
        log.error(e.getMessage());
        return Result.fail(e.getMessage());
    }
    @ExceptionHandler
    public Result exceptionHandler(DataIntegrityViolationException e  ){
        log.error("用户名已被注册，请换个用户名");
        return Result.fail("用户名已被注册，请换个用户名");
    }
    @ExceptionHandler
    public Result  exceptionHandler(NoResourceFoundException e){
        log.error("不用关注favicon异常");
        return Result.fail("不用关注favicon异常");
    }





}
